public class AtomParser {
    // Constructor 
    public AtomParser(string str) { parseFromString(str, null); } 
    public AtomParser(          ) { }
    public AtomParser(String str, String respType) {
    	parseFromString(Str, respType);
    }
    
    private String responseType;
    
    public void parseFromString(string str, String respType) {
    	responseType = respType;
    	System.debug(LoggingLevel.WARN, '\n\nThe String to Parse: \n\n' + str + '\n');
        XmlStreamReader reader = new XmlStreamReader(str);
        reader.setCoalescing(true);
        nodeNames = new Map<String, String>();
        if (reader == null) System.debug(LoggingLevel.ERROR, '\n\n The reader is null.');
        parseXmlReaderNew(root, reader);
    }
     
    integer count = 0 ; 
   
   	public Map<String, String> nodeNames;
   	public void parseXmlReaderNew(AtomBase p, XmlStreamReader reader) {
   		try {
   			AtomBase parent;
   			while (reader.hasNext()) {
   				System.debug(LoggingLevel.INFO, '\n\n Looping...\n' + reader.getLocalName() + '\n');
	            if (reader.getEventType() == XmlTag.START_ELEMENT) {
	   				String nodeName = reader.getLocalName();
	   				nodeNames.put(nodeName, nodeName);
	   				if (responseType != null) nodeName = responseType;
	   				if (nodeName == 'service') {
	   					if (parent == null) { 
	   						parent = new BluehouseService(reader);
	   						p.appendChild(parent);
	   					} 
	   				} else if (nodeName == 'feed') {
	   					if (parent == null) {
	   						parent = new Feed(reader); 
	   						p.appendChild(parent);
	   					}
	   				} else if (nodeName == 'entry') {
	   					if (parent == null) { 
	   						parent = new ActivityEntry(reader);
	   						p.appendChild(parent);
	   					}
	   				} else {
	   					return;
	   				} 
	            } 
	            try {
					reader.next();
	            } catch (Exception e) {
		   			System.debug(LoggingLevel.ERROR, '\n\nERROR Reader.Next():\n\n' + e.getMessage() + '\n');
	            }
  			}
 		} catch(Exception ex) {
			System.debug(LoggingLevel.ERROR, '\n\nERROR Parsing XML:\n\n' + ex.getMessage() + '\n');
   		}
   	}
   	
    // utility dom functions
    public virtual AtomBase ownerDocument() { return root; }

    // everything in the dom is found as childNodes under this root element
    public AtomBase root = new AtomBase('#document');
    public integer debug =0; 
    
}